/**
 * Copyright 2009 Emory University
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated  documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions: The 
 * above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
package com.markgrand.structuredSql;

import scala.collection.mutable.ListBuffer

/**
 * Append a select list to a StringBuilder.   
 * 
 * @param selectExpressions
 *      Value producing constructs that are only allowed in a select list
 * @param valueExpressions
 *      An array of value producing expressions
 * @author Mark Grand
 */
class SelectList protected (val selectExpressions:Seq[SelectValueAppender], val valueExpressions:Seq[SQLValueAppender]) extends SQLAppender {
  if (selectExpressions.length+valueExpressions.length == 0) {
    throw new IllegalArgumentException("Select list must contain at least one item.")
  }

  def append(builder:StringBuilder) {
    if (selectExpressions.length > 0) {
      selectExpressions.addString(builder, ",")
      if (valueExpressions.length > 0) {
        builder.append(",")
      }
    }
    if (valueExpressions.length > 0){
      valueExpressions.addString(builder, ",")
    }
  }
}

object SelectList {
  def apply(selectExpressions:Seq[SelectValueAppender], valueExpressions:Seq[SQLValueAppender]) = {
    new SelectList(selectExpressions, valueExpressions)
  }
  
  /**
   * Create a select list that encapsulates the given expressions.
   * 
   * @param valueExpressions
   *      One or more expressions
   */
  def apply(valueExpressions:SQLValueAppender*): SelectList = new SelectList(Seq[SelectValueAppender](), valueExpressions)
  
  /**
   * Create a select list that encapsulates the given expressions.
   * 
   * @param selectExpressions
   *      Value producing constructs that are only allowed in a select list
   */
  def apply(selectExpression:SelectValueAppender): SelectList = {new SelectList(List(selectExpression), Seq[SQLValueAppender]())}
  
  /**
   * Create a select list that encapsulates the given expressions.
   * 
   * @param columnNames
   *      an Seq of column names
   */
  def apply(columnNames:Array[String], valueExpressions: SQLValueAppender*): SelectList = {
    val columnListBuffer = new ListBuffer[SQLValueAppender]()
    for (columnName <- columnNames) {
      columnListBuffer += TableColumnIdentifier(columnName)
    }
    for (valueExpression <- valueExpressions) {
      columnListBuffer += valueExpression
    }
    apply(columnListBuffer.toList: _*)
  }
  
  
  /**
   * Create a select list that encapsulates the given expressions.
   * 
   * @param columnName
   *      A column name
   */
  def apply(columnName:String): SelectList = apply(TableColumnIdentifier(columnName))
}